package JZOffer;

import java.util.ArrayList;

public class _18 {
    class Solution {
        public ArrayList<Integer> printMatrix(int [][] matrix) {
            if (matrix == null) return new ArrayList();
            int top = 0;
            int bottom = matrix.length - 1;
            int left = 0;
            int right = matrix[0].length - 1;
            ArrayList<Integer> result = new ArrayList();
            while (top <= bottom && left <= right) {
                for (int i = left; i <= right; i++) {
                    result.add(matrix[top][i]);
                }
                top++;
                if (top > bottom) {
                    break;
                }
                for (int i = top; i <= bottom; i++) {
                    result.add(matrix[i][right]);
                }
                right--;
                if (left > right) {
                    break;
                }

                for (int i = right; i >= left; i--) {
                    result.add(matrix[bottom][i]);
                }
                bottom--;
                if (top > bottom) {
                    break;
                }

                for (int i = bottom; i >= top; i--) {
                    result.add(matrix[i][left]);
                }
                left++;
                if (left > right) {
                    break;
                }
            }
            return result;
        }
    }

    Solution getSolution() {
        return new Solution();
    }

    public static void main(String args[]) {
        _18 c = new _18();
        _18.Solution s = c.getSolution();

        System.out.println("finish");

    }
}
